<?php

namespace app\index\controller;

use think\Controller;
use think\Db;

class Plan extends Controller
{
    /**
     * 处理套餐物品解锁
     * */
    public function doViceOrderPlan()
    {
        $list = Db::name('order')->where(['meal_status' => 2, 'is_pay' => 1, 'is_del' => 0, 'is_meal' => 1])->select();
        if ($list) {
            foreach ($list as &$v) {
                Db::name('vice_order_plan')->where(['order_id' => $v['id'], 'status' => 0, 'send_date' => ['elt', date('Y-m-d')]])->setField('status', 1);

                $count = Db::name('vice_order_plan')->where(['order_id' => $v['id'], 'status' => 0])->count();
                if ($count == 0) {
                    // 全部已赠送，状态修改为已完成
                    Db::name('order')->where(['id' => $v['id']])->setField('meal_status', 3);

                    // 更新用户等级
                    updateUserLevel($v['user_id']);
                }
            }
        }
    }

    /**
     * 15天自动完成
     * */
    public function orderAutoGet()
    {
        $list = Db::name('order')->where(['type' => 1, 'status' => 3, 'update_time' => ['elt', strtotime('-15 days', time())]])->select();
        if($list){
            foreach($list as $v){
                 Db::name('order')->where(['id' => $v['id']])->update(['status' => 4, 'update_time' => time()]);
            }
        }
    }
}
